package com.zy.web.controller.bs;

import com.zy.entity.bs.Notice;
import com.zy.pojo.constants.Constant;
import com.zy.pojo.exceptions.ControllerException;
import com.zy.pojo.query.bs.NoticeQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.pojo.vos.common.ResultVo;
import com.zy.service.bs.NoticeService;
import com.zy.utils.ValidUtil;
import com.zy.web.controller.base.BaseController;
import com.zy.web.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RequestMapping({"bs/notice","m/bs/notice"})
@Controller
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;

	/**
	 * 分页
 	 * 
 	 * @param noticeQuery
	 * @param start
	 * @param limit
	 * @return
	 */
	@RequestMapping("pageNotice")
	@ResponseBody
	public PageVo pageNotice(NoticeQuery noticeQuery, Integer start, Integer limit) {
		return this.noticeService.pageNotice(noticeQuery, start, limit);
	}

	/**
	 * 新增
	 * 
	 * @param notice
	 * @return
	 */
	@RequestMapping(value = "saveNotice", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo saveNotice(Notice notice) {
		try {
			notice.setCreateUserId(super.getCurrentUser().getId());
			this.noticeService.saveNotice(notice);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}
	
	/**
	 * 更新
	 * 
	 * @param notice
	 * @return
	 */
	@RequestMapping(value = "updateNotice", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo updateNotice(Notice notice) {
		try {
			notice.setModifyUserId(super.getCurrentUser().getId());
			this.noticeService.updateNotice(notice);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "removeNotice", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo removeNotice(long id) {
		try {
			this.noticeService.removeNotice(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 恢复
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "recover", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo recover(long id) {
		try {
			this.noticeService.recover(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 根据公告类型获取公告数据
	 *
	 * @param id 	类型id
	 * @return
	 */
	@RequestMapping(value = "front/getNoticeForApp", method = RequestMethod.GET)
	@ResponseBody
	public ResultVo getNoticeForApp(Long id) {
		try {
			Notice notice = new Notice();
		    if(Objects.isNull(id))
				notice.setType(Constant.NOTICEID);
			else
				notice.setType(id);
			return ResultUtil.success((Serializable)this.noticeService.list(notice));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	@RequestMapping(value = "front/getInfo", method = RequestMethod.GET)
	@ResponseBody
	public ResultVo getInfo(Long id) {
		try {
			return ResultUtil.success(this.noticeService.getByPrimaryKey(id));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}
}